package com.adarsh.f1.struts.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.adarsh.f1.entity.Team;
import com.adarsh.f1.struts.form.LoginForm;

public class LoginAction extends DispatchAction {
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
		String action = mapping.getParameter();
		
		if(action.equals("loginAction"))
			return login(mapping, form,request,response);
		else if(action.equals("showTime"))
			return showTime(mapping, form,request,response);
		else
			return null;
	}

	private ActionForward showTime(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		printWriter.append((new Date()).toString());
		printWriter.flush();
		printWriter.close();
		return null;
	}

	private ActionForward login(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		Connection conn = null;
		Statement statement = null;
		ResultSet resultSet = null;
		
		LoginForm loginForm = (LoginForm)form;
		
		try {
			Context initContext = new InitialContext();
			Context envContext  = (Context)initContext.lookup("java:/comp/env");
			DataSource ds = (DataSource)envContext.lookup("jdbc/TestDB");
			conn = ds.getConnection();
			statement = conn.createStatement();
			resultSet = statement.executeQuery("SELECT * FROM DRIVER");
			
			while(resultSet.next()){
				System.out.println(resultSet.getString("DRIVER_NAME"));
			}
			
			
			//boolean flag = statement.execute("UPDATE DRIVER SET DRIVER_NAME='Sergio Perez' WHERE DRIVER_NAME='Perez'");
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			try {
				resultSet.close();
				statement.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		



		
		if(loginForm.getUserName().equals("Adarsh") && loginForm.getPassword().equals("a"))
			return mapping.findForward("success");
		else
			return mapping.findForward("continue");
		
	}
}
